package TCPMain;

import org.json.JSONObject;

// 定义一个类 TransferProgress，用于跟踪文件传输进度
// 在TCPMain包中创建FileProgress.java
public class FileProgress {
    private String currentFileName;
    private int progress; // 0-100
    private long totalSize;
    private long downloadedSize;

    // 添加同步方法synchronized保证线程安全
    public synchronized void update(String fileName, long downloaded, long total) {
        this.currentFileName = fileName;
        this.downloadedSize = downloaded;
        this.totalSize = total;
        this.progress = (int) ((downloaded * 100) / total);
    }

    // Getters
    public synchronized String getCurrentFileName() { return currentFileName; }
    public synchronized int getProgress() { return progress; }
    public synchronized long getDownloadedSize() { return downloadedSize; }
    public synchronized long getTotalSize() { return totalSize; }
    public String toString() {
        JSONObject json = new JSONObject();
        try{
            json.put("currentFileName", currentFileName);
            json.put("progress", progress);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json.toString();
    }
}
